package controller

import (
	"EnjoyWorking/model"
	"EnjoyWorking/utils"
	"EnjoyWorking/web/service"
	"github.com/kataras/golog"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"
	"strconv"
	"strings"
	"time"
)

func NewWprController(app *iris.Application, party iris.Party) {
	var (
		user       = party.Party("/wpr")
		controller = Wpr{service: service.Wpr{}}
	)

	user.Post("/create", hero.Handler(controller.create)).Name = "创建工作计划总结"
	user.Get("/getAll", hero.Handler(controller.getAll)).Name = "获取所有工作计划总结"
	user.Get("/get", hero.Handler(controller.get)).Name = "获取单个工作计划总结"
	user.Put("/update", hero.Handler(controller.update)).Name = "编辑工作计划总结"
	user.Delete("/delete", hero.Handler(controller.delete)).Name = "删除工作计划总结"

	user.Get("/getByUserID", hero.Handler(controller.getByUserID)).Name = "获取用户所有的工作计划总结"

	user.Post("/check", hero.Handler(controller.check)).Name = "审核工作计划总结"

	list := app.GetRoutes()
	for _, v := range list {
		if v.Method == "OPTIONS" {
			continue
		}
		if strings.Index(v.Path, "/api/wpr") != -1 {
			v.Description = "工作计划总结管理接口"
		}
	}
}

type Wpr struct {
	service service.Wpr
}

func (c *Wpr) check(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		id   int64
		uid  uint
	)

	if id, err = ctx.URLParamInt64("id"); err != nil {
		goto ERR
	}

	if uid, err = ctx.Values().GetUint(utils.UID); err != nil {
		goto ERR
	}

	if err = c.service.Check(uint(id), uid, ctx.URLParam("comments")); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_WPR_CHECK_OK)
	return
ERR:
	golog.Errorf("工作计划总结[%d]审核失败。原因：%s，错误：%s", id, code, err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Wpr) create(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		vo = new(model.Wpr)
		now = time.Now()
	)

	if err = ctx.ReadJSON(&vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = utils.Validator().Struct(vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	vo.CreateDate =  time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)

	vo.CheckTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local)

	if err = c.service.Insert(vo); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_WPR_CREATE_OK)
	return
ERR:
	golog.Errorf("工作计划总结[%d]创建失败。原因：%s，错误：%s", vo.MakerID, code, err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Wpr) getByUserID(ctx iris.Context) {
	var (
		code       utils.Code
		err        error
		uid        int64
		total      int64
		result     []*model.Wpr
		resultCopy []*model.WprCopy
	)

	if uid, err = ctx.URLParamInt64("id"); err != nil {
		goto ERR
	}

	if total, result, err = c.service.FindByUserId(uint(uid)); err != nil {
		goto ERR
	}

	for _, v := range result {
		resultCopy = append(resultCopy, &model.WprCopy{*v})
	}

	utils.PaginationTableData(ctx, total, resultCopy)
	return
ERR:
	golog.Errorf("失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Wpr) getAll(ctx iris.Context) {

}

func (c *Wpr) get(ctx iris.Context) {
}

func (c *Wpr) update() {

}

func (c *Wpr) delete(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		ids  []int64
		uid  uint
	)

	if ids, err = func() (result []int64, err error) {
		idStr := strings.Split(ctx.URLParam("ids"), ",")
		for _, v := range idStr {
			var id int64
			if id, err = strconv.ParseInt(v, 10, 64); err != nil {
				code = utils.CODE_SYS_PARSE_PARAMS_ERROR
				return nil, err
			}
			result = append(result, id)
		}
		return
	}(); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if uid, err = ctx.Values().GetUint(utils.UID); err != nil {
		goto ERR
	}

	if err = c.service.Delete(ids, uid); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_WPR_DELETE_OK)
	return
ERR:
	golog.Errorf("工作计划总结删除失败。原因：%s，错误：%s", code, err)
	utils.InternalServerError(ctx, code, err)
}


